﻿
using Admin.NET.Application.Entity;
using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;

/// <summary>
/// 变量历史值服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class DevicePointsValueHistoryService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<DevicePointsValueHistory> _devicePointsValueHistoryRep;
    public DevicePointsValueHistoryService(SqlSugarRepository<DevicePointsValueHistory> devicePointsValueHistoryRep)
    {
        _devicePointsValueHistoryRep = devicePointsValueHistoryRep;
    }

    /// <summary>
    /// 分页查询变量历史值
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询变量历史值")]
    public async Task<SqlSugarPagedList<DevicePointsValueHistoryOutput>> Page(PageDevicePointsValueHistoryInput input)
    {
        var query = _devicePointsValueHistoryRep.AsQueryable()
            .WhereIF(input.DeviceInfoId>0, u => u.DeviceInfoId == input.DeviceInfoId)
            .WhereIF(input.Id>0,u=>u.DeviceSlaveTemplatePointsId==input.Id)
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<DeviceInfo>((u, deviceinfoid) => u.DeviceInfoId == deviceinfoid.Id )
            .LeftJoin<DeviceSlaveTemplatePoints>((u, deviceinfoid, deviceslavetemplatepointsid) => u.DeviceSlaveTemplatePointsId == deviceslavetemplatepointsid.Id )
            .Select((u, deviceinfoid, deviceslavetemplatepointsid) => new DevicePointsValueHistoryOutput
            {
                Id = u.Id,
                DeviceInfoId = u.DeviceInfoId, 
                DeviceInfoIdDeviceName = deviceinfoid.DeviceName,
                DeviceSlaveTemplatePointsId = u.DeviceSlaveTemplatePointsId, 
                DeviceSlaveTemplatePointsIdPointName = deviceslavetemplatepointsid.PointName,
                PonitValue = u.PonitValue,
                CreateTime = u.CreateTime,
                UpdateTime = u.UpdateTime,
                CreateUserId = u.CreateUserId,
                CreateUserName = u.CreateUserName,
                UpdateUserId = u.UpdateUserId,
                UpdateUserName = u.UpdateUserName,
                IsDelete = u.IsDelete,
                ReadValueType = u.ReadValueType,
            });
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加变量历史值
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加变量历史值")]
    public async Task<long> Add(AddDevicePointsValueHistoryInput input)
    {
        var entity = input.Adapt<DevicePointsValueHistory>();
        await _devicePointsValueHistoryRep.CopyNew().InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除变量历史值
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除变量历史值")]
    public async Task Delete(DeleteDevicePointsValueHistoryInput input)
    {
        var entity = await _devicePointsValueHistoryRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _devicePointsValueHistoryRep.FakeDeleteAsync(entity);   //假删除
        //await _devicePointsValueHistoryRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新变量历史值
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新变量历史值")]
    public async Task Update(UpdateDevicePointsValueHistoryInput input)
    {
        var entity = input.Adapt<DevicePointsValueHistory>();
        await _devicePointsValueHistoryRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取变量历史值
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取变量历史值")]
    public async Task<DevicePointsValueHistory> Detail([FromQuery] QueryByIdDevicePointsValueHistoryInput input)
    {
        return await _devicePointsValueHistoryRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取变量历史值列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取变量历史值列表")]
    public async Task<List<DevicePointsValueHistoryOutput>> List([FromQuery] PageDevicePointsValueHistoryInput input)
    {
        return await _devicePointsValueHistoryRep.AsQueryable().Select<DevicePointsValueHistoryOutput>().ToListAsync();
    }

    /// <summary>
    /// 获取设备ID列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "DeviceInfoDeviceInfoIdDropdown"), HttpGet]
    [DisplayName("获取设备ID列表")]
    public async Task<dynamic> DeviceInfoDeviceInfoIdDropdown()
    {
        return await _devicePointsValueHistoryRep.Context.Queryable<DeviceInfo>()
                .Select(u => new
                {
                    Label = u.DeviceName,
                    Value = u.Id
                }
                ).ToListAsync();
    }
    /// <summary>
    /// 获取变量ID列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "DeviceSlaveTemplatePointsDeviceSlaveTemplatePointsIdDropdown"), HttpGet]
    [DisplayName("获取变量ID列表")]
    public async Task<dynamic> DeviceSlaveTemplatePointsDeviceSlaveTemplatePointsIdDropdown()
    {
        return await _devicePointsValueHistoryRep.Context.Queryable<DeviceSlaveTemplatePoints>()
                .Select(u => new
                {
                    Label = u.PointName,
                    Value = u.Id
                }
                ).ToListAsync();
    }




}
